package com.lyj.sc.book.zhanheduilie;

import java.util.Stack;

/**
 * @program: code-random
 * @description: 编写一个类，用两个栈实现队列，支持队列的基本操作（add、poll、peek）。
 * @author: lyj
 * @create: 2022-07-08 11:57
 * @version: 1.0
 * @Modifier By:
 **/
public class queueconsistingoftwostacks {
    //入
    private Stack<Integer> stack1;
    //出
    private Stack<Integer> stack2;

    public queueconsistingoftwostacks(){
        stack1= new Stack<Integer>();
        stack2 = new Stack<Integer>();
    }

    public void add(Integer i){
        //先stack1加
        stack1.push(i);
    }

    public Integer poll(){
        if(!this.stack2.isEmpty()){
            return stack2.pop();
        }
        while (!stack1.isEmpty()){
            Integer pop = stack1.pop();
            stack2.push(pop);
        }
        return stack2.pop();
    }

    public Integer peek(){
        if(!this.stack2.isEmpty()){
            return stack2.peek();
        }
        while (!stack1.isEmpty()){
            Integer pop = stack1.pop();
            stack2.push(pop);
        }
        return stack2.peek();
    }

}
